package com.example.mydy.withdraw;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.mydy.R;
import com.example.mydy.model.UserInfo;
import com.example.mydy.utils.MockDataManager;
import com.google.android.material.textfield.TextInputEditText;

public class WithdrawStep1Fragment extends Fragment implements WithdrawStepListener {

    private TextView tvBalance;
    private TextView tvWithdrawAll;
    private TextInputEditText etWithdrawAmount;
    private TextView tvAmountError;

    private double availableBalance = 0.0;
    private WithdrawDataManager dataManager;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_withdraw_step1, container, false);
        
        // 初始化视图
        tvBalance = view.findViewById(R.id.tv_balance);
        tvWithdrawAll = view.findViewById(R.id.tv_withdraw_all);
        etWithdrawAmount = view.findViewById(R.id.et_withdraw_amount);
        tvAmountError = view.findViewById(R.id.tv_amount_error);
        
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        
        // 获取数据管理器
        dataManager = WithdrawDataManager.getInstance();
        
        // 加载余额数据
        loadBalanceData();
        
        // 设置全部提现按钮点击事件
        tvWithdrawAll.setOnClickListener(v -> {
            etWithdrawAmount.setText(String.format("%.2f", availableBalance));
        });
    }

    private void loadBalanceData() {
        // 获取当前用户
        UserInfo currentUser = MockDataManager.getInstance().getCurrentUser();
        if (currentUser != null) {
            try {
                availableBalance = Double.parseDouble(currentUser.getBalance());
            } catch (NumberFormatException e) {
                availableBalance = 0.0;
            }
            
            // 更新余额显示
            tvBalance.setText(String.format("¥ %.2f", availableBalance));
        }
    }

    @Override
    public boolean validate() {
        // 验证提现金额
        String amountStr = etWithdrawAmount.getText().toString().trim();
        if (TextUtils.isEmpty(amountStr)) {
            showError("请输入提现金额");
            return false;
        }
        
        double amount;
        try {
            amount = Double.parseDouble(amountStr);
        } catch (NumberFormatException e) {
            showError("提现金额格式不正确");
            return false;
        }
        
        // 验证最低提现金额
        if (amount < WithdrawActivity.MINIMUM_WITHDRAWAL) {
            showError("最低提现金额为" + WithdrawActivity.MINIMUM_WITHDRAWAL + "元");
            return false;
        }
        
        // 验证是否超过可提现余额
        if (amount > availableBalance) {
            showError("提现金额不能超过可用余额");
            return false;
        }
        
        // 通过验证，保存数据
        dataManager.setWithdrawAmount(amount);
        return true;
    }

    private void showError(String message) {
        tvAmountError.setText(message);
        tvAmountError.setVisibility(View.VISIBLE);
    }

    @Override
    public void onResume() {
        super.onResume();
        
        // 重置错误提示
        tvAmountError.setVisibility(View.GONE);
        
        // 如果已经设置了提现金额，则恢复
        double savedAmount = dataManager.getWithdrawAmount();
        if (savedAmount > 0) {
            etWithdrawAmount.setText(String.format("%.2f", savedAmount));
        }
    }
} 